package org.rsbot.bot;

import java.awt.AWTPermission;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketPermission;
import java.net.URL;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.Permissions;
import java.security.ProtectionDomain;
import java.util.Calendar;
import java.util.Map;
import java.util.PropertyPermission;

public final class RSClassLoader extends ClassLoader
{
  private Map<String, byte[]> classes;
  private ProtectionDomain domain;

  public RSClassLoader(Map<String, byte[]> paramMap, URL paramURL)
  {
    try
    {
      CodeSource localCodeSource = new CodeSource(paramURL, (CodeSigner[])null);
      this.domain = new ProtectionDomain(localCodeSource, getPermissions());
      this.classes = paramMap;

      String str = getClass().getResource("RSClassLoader.class").toString();
      str = str.replace("bot/RSClassLoader.class", "client/RandomAccessFile.class");
      URL localURL = new URL(str);

      BufferedInputStream localBufferedInputStream = null;
      try {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(5000);
        localBufferedInputStream = new BufferedInputStream(localURL.openStream());

        byte[] arrayOfByte1 = new byte[1024];
        int i = -1;
        while ((i = localBufferedInputStream.read(arrayOfByte1)) != -1) {
          localByteArrayOutputStream.write(arrayOfByte1, 0, i);
        }
        byte[] arrayOfByte2 = localByteArrayOutputStream.toByteArray();

        this.classes.put("org.rsbot.client.RandomAccessFile", arrayOfByte2);
      } catch (IOException localIOException) {
        localIOException.printStackTrace();
      } finally {
        if (localBufferedInputStream != null)
          localBufferedInputStream.close(); 
      }
    } catch (Exception localException) {
    }
  }

  private Permissions getPermissions() {
    Permissions localPermissions = new Permissions();
    localPermissions.add(new AWTPermission("accessEventQueue"));
    localPermissions.add(new PropertyPermission("user.home", "read"));
    localPermissions.add(new PropertyPermission("java.vendor", "read"));
    localPermissions.add(new PropertyPermission("java.version", "read"));
    localPermissions.add(new PropertyPermission("os.name", "read"));
    localPermissions.add(new PropertyPermission("os.arch", "read"));
    localPermissions.add(new PropertyPermission("os.version", "read"));
    localPermissions.add(new SocketPermission("*", "connect,resolve"));
    String str1 = System.getProperty("user.home");
    if (str1 != null)
      str1 = str1 + "/";
    else {
      str1 = "~/";
    }
    String[] arrayOfString1 = { "c:/rscache/", "/rscache/", "c:/windows/", "c:/winnt/", "c:/", str1, "/tmp/", "." };
    String[] arrayOfString2 = { ".jagex_cache_32", ".file_store_32" };
    for (String str2 : arrayOfString1) {
      File localFile = new File(str2);
      localPermissions.add(new FilePermission(str2, "read"));
      if (!localFile.exists()) {
        continue;
      }
      str2 = localFile.getPath();
      for (String str3 : arrayOfString2) {
        localPermissions.add(new FilePermission(str2 + File.separator + str3 + File.separator + "-", "read"));
        localPermissions.add(new FilePermission(str2 + File.separator + str3 + File.separator + "-", "write"));
      }
    }
    Calendar.getInstance();

    localPermissions.setReadOnly();
    return localPermissions;
  }

  public final Class<?> loadClass(String paramString) throws ClassNotFoundException
  {
    if (this.classes.containsKey(paramString)) {
      byte[] arrayOfByte = (byte[])this.classes.remove(paramString);
      return defineClass(paramString, arrayOfByte, 0, arrayOfByte.length, this.domain);
    }
    return super.loadClass(paramString);
  }
}